package com.aric.springbootmusic.controller;

import com.aric.springbootmusic.entity.dto.AddRoleDTO;
import com.aric.springbootmusic.entity.param.EditRoleParam;
import com.aric.springbootmusic.entity.param.RoleAuthorParam;
import com.aric.springbootmusic.entity.pojo.Role;
import com.aric.springbootmusic.entity.param.AddRoleParam;
import com.aric.springbootmusic.exceptions.NameDuplicationException;
import com.aric.springbootmusic.service.RoleService;
import com.aric.springbootmusic.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 需要的权限如下
     *
     * 权限名称  权限id    权限等级      权限父id
     *
     * 权限管理    1      0(level)     0(父id)------一级 (AuthorityManager)
     * 角色列表    2      1(level)     1(父id)------二级 (RoleList)
     * 添加角色    3      2(level)     2(父id)------三级 (AddRole)
     * 删除角色    4      2(level)     2(父id)------三级 (DeleteRole)
     * 更新角色    5      2(level)     2(父id)------三级 (UpdateRole)
     * 查询角色    6      2(level)     2(父id)------三级 (SelectRole)
     * 角色授权    7      2(level)     2(父id)------三级 (RoleAuthority)
     *
     * 权限列表    8      1(level)     1(父id)------二级 (RoleAuthorityList)
     *
     * 用户管理    9      0(level)     0(父id)------一级 (UserManager)
     * 用户列表    10     1(level)     9(父id)------二级 (UserList)
     * 添加用户    11     2(level)     10(父id)-----三级 (AddUser)
     * 更新用户    12     2(level)     10(父id)-----三级 (UpdateUser)
     * 查询用户    13     2(level)     10(父id)-----三级 (SelectUser)
     * 删除用户    14     2(level)     10(父id)-----三级 (DeleteUser)
     *
     * 音乐管理    15     0(level)     0(父id)------一级 (MusicManager)
     * 音乐列表    16     1(level)     15(父id)-----二级 (MusicList)
     * 添加音乐    17     2(level)     16(父id)-----三级 (AddMusic)
     * 删除音乐    18     2(level)     16(父id)-----三级 (DeleteMusic)
     * 更新音乐    19     2(level)     16(父id)-----三级 (UpdateMusic)
     * 查询音乐    20     2(level)     16(父id)-----三级 (SelectMusic)
     */
    @Autowired
    private RoleService roleService;

    /**
     * 添加角色 没有处理重名
     */
    @PreAuthorize("hasAuthority('AddRole')")
    @PostMapping("/addRole")
    public Result addRole(@Validated @RequestBody AddRoleParam addRoleParam) throws NameDuplicationException {
        AddRoleDTO roleDTO = AddRoleDTO.builder()
                .roleName(addRoleParam.getRoleName())
                .roleDesc(addRoleParam.getRoleDesc())
                .isDelete(0)
                .build();
        return roleService.addNewRole(roleDTO);
    }

    /**
     * 删除角色
     */
    @PreAuthorize("hasAuthority('DeleteRole')")
    @DeleteMapping("/deleteRole/{id}")
    public Result deleteRole(@PathVariable("id") Integer id){
        return roleService.deleteRole(id);
    }

    /**
     * 更新角色  没有处理重名
     */
    @PreAuthorize("hasAuthority('UpdateRole')")
    @PutMapping("/updateRole")
    public Result updateRole(@Validated @RequestBody EditRoleParam roleParam){
        Role role = Role.builder()
                .roleId(roleParam.getId())
                .roleName(roleParam.getRoleName())
                .roleDesc(roleParam.getRoleDesc())
                .build();
        return roleService.updateRoleById(role);
    }

    /**
     * 查询单个角色
     */
    @PreAuthorize("hasAuthority('SelectRole')")
    @GetMapping("/selectSingleRole")
    public Result selectSingleRole(@RequestParam Integer id){
        return roleService.findRoleById(id);
    }

    /**
     * 查询所有角色  迁移到User Controller
     */
    @PreAuthorize("hasAnyAuthority('RoleList','AddUser')")
    @GetMapping("/selectAllRole")
    public Result selectAllRole(@RequestParam Integer currentPage,Integer pageSize){
        return roleService.findAllRole(currentPage,pageSize);
    }

    @PreAuthorize("hasAnyAuthority('AddUser')")
    @GetMapping("/selectRoles")
    public Result selectRoles(){
        return roleService.findRoles();
    }

    /**
     * 角色授权
     */
    @PreAuthorize("hasAuthority('RoleAuthority')")
    @PostMapping ("/roleAuthorize")
    public Result roleAuthorize(@RequestBody RoleAuthorParam roleAuthorParam){
        List<Integer> psIds = Arrays.stream(roleAuthorParam.getPsIds()).toList();
        Integer roleId=roleAuthorParam.getId();
        return roleService.updateRoleAuthor(roleId,psIds);
    }
}
